home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / 0748.ZIP / GLOBSUB < prev    next >
Text File  |  1986-12-29  |  2KB  |  53 lines

  1. /* This is the global substitute command - it simulates the global subst */
  2. /* command found in the New York Word word processor (also sold by MAGMA */
  3. /* SYSTEMS). */
  4. /* You first prepare an external file (called "subs" here) that contains */
  5. /* lines of the form :   pattern=sub text */
  6. /* The last line of the file should be a blank line. Then you hit the */
  7. /* <CTRL> G key, and the substitutions are performed. */
  8. /* Written by Marc Adler  8/86 */
  9.  
  10. init()
  11. {
  12.   assign_key("global_sub", 7);    /* <CTRL> G */
  13. }
  14.  
  15.  
  16. global_sub()
  17. {
  18.   string search_pat, sub_text, foo;
  19.   int    old_buf, sub_buf, i;
  20.  
  21.   save_position();
  22.  
  23.   /* Read in the substitutions */
  24.   old_buf = currbuf();
  25.   sub_buf = setcurrbuf(create_buffer("subs"));
  26.  
  27.   while ((i = index(currline(), "=")) > 0)      /* still a pattern? */
  28.   {
  29.     /* Isolate both the pattern and the substitution text  */
  30.     search_pat = substr(currline(), 1, i - 1);
  31.     sub_text = substr(currline(), i + 1, strlen(currline()));
  32.  
  33. /*   Uncomment the following line if you want to check the patterns */
  34. /*   foo = get_tty_str(sprintf("srch <%s>, sub <%s>", search_pat, sub_text)); */
  35.  
  36.     /* Go to the original buffer, and perform a forward substitution without */
  37.     /* user approval. To get approval, change the 3rd param of fsubst to 1.  */
  38.     setcurrbuf(old_buf);
  39.     gobof();
  40.     fsubst(search_pat, sub_text, 0);
  41.  
  42.     /* Go to the substitution buffer and move down to the next pattern. */
  43.     setcurrbuf(sub_buf);
  44.     if (!down())  break;
  45.   }
  46.  
  47.   /* Get rid of the substitution buffer and re-enable the original buffer */
  48.   delete_buffer(sub_buf);
  49.   setcurrbuf(old_buf);
  50.   restore_position();
  51.   foo = get_tty_str("Finished with substitutions");
  52. }
  53.